const workRecords = require("../models/workRecords")
const user = require("../models/userDetail")

var express = require('express');
var router = express.Router();
//  知识点标题  题目信息  应作人数  答对人数 正确率  题型
const formidable = require('formidable');

router.post('/', (req, res, next) => {
  var form = new formidable.IncomingForm();
  form.parse(req, function (err, fields, files) {
    if (err) {
        res.json({ 'state': false })
    } else {
      workRecords.find({
        seq: fields.seq
      }, async(err, docs) => {
        if (err) {
          res.json({ 'state': false, 'msg': err });
        } else{
          // console.log(docs)
          if(docs.length===0){
            res.json({ 'state': false, 'msg': '暂时没有学生提交作业' });
          }
          let stu_list = []
          async function find_mes() {
            for(let k=0; k<docs.length; k++) {
              stu_list[k] = {}
              stu_list[k].UUID = docs[k].UUID
              let stu = await user.find({
                UUID: docs[k].UUID
              })
              // console.log(stu)
              stu_list[k].realName = stu[0].realName
              stu_list[k].stuNo = stu[0].stuNo
            }
            return stu_list
          }
          find_mes().then(result => {
            res.json({ 'state': true, 'data': result });
          })
         
        }
      })
    }
  })
})

module.exports = router;